** Figure 7 **
cd "D:\Dropbox\unequal_gains\QJE revision plan\analysis\"
global resultspath "D:\Dropbox\unequal_gains\QJE revision plan\analysis\clean_results"

* 1. Finalize data
use "$db/Important Datasets/2004_spending_by_income_quintiles.dta", clear
keep if income_quintile==5 | income_quintile==1
bysort product_module_code quality_rank: egen double allspending=sum(total_spending)
gen share=total_spending/allspending
keep if income_quintile==5

merge m:1 product_module_code quality_rank using instrument_income_quintiles
keep if _merge==3
drop _merge

* rescale outcome variables so that they are equal to "average";
* we take average inflation for 3rd quintile as the average 
gen infl_hat_mean=1.457098
gen full_infl_hat_mean=-0.02124015

sum infl_hat_l [aw=allspending], d
replace infl_hat_l=infl_hat_l-r(mean)+infl_hat_mean

sum full_infl_hat_l [aw=allspending], d
replace full_infl_hat_l=full_infl_hat_l-r(mean)+full_infl_hat_mean

replace share=share*100 

* 2. Panel A.a

reg infl_hat_l share [aw=allspending], cluster(product_module_code)
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
display "`b'"
display "`se'"

binscatter infl_hat_l share [aw=allspending], reportreg nq(100) ///
ytitle("Predicted Annual Tornqvist Inflation" "for Continued Products (%)") graphregion(color(white)) ///
note("Coeff. `b' ***" "s.e. `se'", span ring(0) pos(12) size(med) box bfc(white)) ///
xtitle("Share of Sales to Top Income Quintile relative to Bottom Income Quintile (%)") ylabel(,angle(0))
graph export "$resultspath/Figure7A_a.pdf", as(pdf) replace
graph save "$resultspath/Figure7A_a.gph", replace

* 3. Panel A.b 

reg full_infl_hat_l share [aw=allspending], cluster(product_module_code)
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
display "`b'"
display "`se'"

binscatter full_infl_hat_l share [aw=allspending], reportreg nq(100) ///
note("Coeff. `b' ***" "s.e. `se'", span ring(0) pos(12) size(med) box bfc(white)) ///
ytitle("Predicted Annual CES Inflation," "Accounting for Changes in Product Variety (%)") graphregion(color(white)) ///
xtitle("Share of Sales to Top Income Quintile relative to Bottom Income Quintile (%)") ylabel(,angle(0))
graph export "$resultspath/Figure7A_b.pdf", as(pdf) replace
graph save "$resultspath/Figure7A_b.gph", replace

* 4. Compute patterns across income groups
clear
set obs 1
gen infl_diff_continued=0
gen infl_diff_full=0
gen incq=5
save  "$db/Important Datasets/calibration_results", replace

foreach i in 5 4 3 2 {

use "$db/Important Datasets/2004_spending_by_income_quintiles.dta", clear

keep if income_quintile==`i' | income_quintile==`i'-1
bysort product_module_code quality_rank: egen double allspending=sum(total_spending)
gen share=total_spending/allspending
keep if income_quintile==`i'-1

merge m:1 product_module_code quality_rank using instrument_income_quintiles
keep if _merge==3
drop _merge

reg infl_hat_l share [aw=allspending]
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'

sum share [aw=sqrt(allspending)], d
gen infl_diff_continued=r(Var)/(r(mean)*(1-r(mean)))*`b'

reg full_infl_hat_l share [aw=allspending]
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'

sum share [aw=sqrt(allspending)], d
gen infl_diff_full=r(Var)/(r(mean)*(1-r(mean)))*`b'

keep infl_diff*
duplicates drop
gen incq=`i'-1
append using "$db/Important Datasets/calibration_results"
save "$db/Important Datasets/calibration_results", replace

}

use "$db/Important Datasets/calibration_results", clear
gsort -incq
gen infl_diff_continued_final=sum(infl_diff_continued)
gen infl_diff_full_final=sum(infl_diff_full)

save "$db/Important Datasets/calibration_results", replace

* 5. Panels B and C
use "$db/Important Datasets/calibration_results", clear

scatter infl_diff_continued_final incq || ///
lfit infl_diff_continued_final incq, lcolor(maroon) graphregion(color(white)) ///
 xtitle("Household Income Quintile") ytitle("Inflation Difference Relative to Top Income Quintile," ///
 "Tornqvist Inflation for Continued Products (pp)", size(small)) ylabel(0(0.1)0.4,angle(0)) leg(off)
graph export "$resultspath/Figure7B.pdf", as(pdf) replace
graph save "$resultspath/Figure7B.gph", replace

scatter infl_diff_full_final incq || ///
lfit infl_diff_full_final incq, lcolor(maroon) graphregion(color(white)) ///
 xtitle("Household Income Quintile") ytitle("Inflation Difference Relative to Top Income Quintile," ///
 "CES Inflation with Product Variety Adj. (pp)", size(small)) ylabel(0(0.1)0.6,angle(0)) leg(off)
graph export "$resultspath/Figure7C.pdf", as(pdf) replace
graph save "$resultspath/Figure7C.gph", replace

drop infl_diff_full infl_diff_continued
save "$db/Important Datasets/calibration_results", replace
